package com.dotstone.chipism.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.dotstone.chipism.R;
import com.dotstone.chipism.bean.Device;
import com.dotstone.chipism.bean.MySocket;
import com.dotstone.chipism.bean.MySocket.RecDataCallBackListener_Serach;
import com.dotstone.chipism.dialog.SVProgressHUD;
import com.dotstone.chipism.listener.OnDeviceStatusListener;
import com.dotstone.chipism.util.ConvertUtil;
import com.dotstone.chipism.util.DeviceManager;
import com.dotstone.chipism.util.SocketManager;
import com.dotstone.chipism.util.ToastShow;
import com.dotstone.chipism.util.Util;
import com.dotstone.chipism.view.slidingfragmenu.BarUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class RemoteRGBLightActivity extends BaseActivity {
	private LinearLayout mBackL;
	private TextView mStatusBar;
	private Button mPowerB;
	private TextView mTitleTv;
	private boolean power = false;
	private Button sceneB1;
	private Button sceneB2;
	private Button sceneB3;
	private Button sceneB4;
	private Button sceneB5;
	private Button sceneB6;
	private RelativeLayout mF;
	private RelativeLayout mF1;
	private RelativeLayout mS;
	private RelativeLayout mS1;
	private int o_x;
	private int o_y;
	private static int dpi;
	private float current_degree;
	private int tempX1;
	private int tempY1;
	private Button mJump;
	private long lastTime;
	private long currentTime;
	private long lastTime1;
	private long currentTime1;
	private float level = 100;
	private int pixel;
	private Drawable d;
	private Bitmap bm;
	private int r;
	private int g;
	private int b;
	private int rgb[] = new int[4];
	private int inner;
	private String address;
	private String id;
	private Device device;
	private MySocket socket;
	private byte[] deviceAdd;
	private byte[] data = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x82, (byte) 0x00, 0x00, 0x00 };
	private byte[] data1 = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x8b, (byte) 0x00, 0x00, 0x00 };
	private int[] intCmd;
	private int[] intCmd1;
	private String strCmd;
	private float percent_ness;
	private List<Button> buttons = new ArrayList<Button>();
	private SVProgressHUD mSVProgressHUD;
	private boolean needToCheck = true;
	private int times = 0;
	private boolean received = false;
	@Override
	public void widgetClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_layout:
			finish();
			break;
		case R.id.power_b:
			if (power) { // 当前打开 发送关指令
				mPowerB.setBackgroundResource(R.drawable.kaiguan22x);
				// socket.sendMqttData(device.getCloseCmd());
				sendCmd(device.getCloseCmd());
				setBg(7);
			} else { // 当前关闭 发送开的指令
				mPowerB.setBackgroundResource(R.drawable.kaiguan12x);
				// socket.sendMqttData(device.getOpenCmd());
				sendCmd(device.getOpenCmd());
				setBg(7);
			}
			power = !power;
			DeviceManager.getInstance().setDeviceState(device.getDeviceID(), power);
			break;
		case R.id.more_btn:
			// 显示选择弹出框
			//
			break;
		case R.id.scene_btn1:
			// Log.i("wmy", "心跳");
			data[7] = (byte) 0x70;
			intCmd = Util.byte2HexStr(data);
			strCmd = ConvertUtil.getInstance().transLightCmd(intCmd);
			// socket.sendMqttData(strCmd);
			sendCmd(strCmd);
			setBg(0);
			break;
		case R.id.scene_btn2:
			// Log.i("wmy", "缤纷");
			data[7] = (byte) 0x60;
			intCmd = Util.byte2HexStr(data);
			strCmd = ConvertUtil.getInstance().transLightCmd(intCmd);
			// socket.sendMqttData(strCmd);
			sendCmd(strCmd);
			setBg(1);
			break;
		case R.id.scene_btn3:
			// Log.i("wmy", "霓虹");
			data[7] = (byte) 0x41;
			intCmd = Util.byte2HexStr(data);
			strCmd = ConvertUtil.getInstance().transLightCmd(intCmd);
			// socket.sendMqttData(strCmd);
			sendCmd(strCmd);
			setBg(2);
			break;
		case R.id.scene_btn4:
			// Log.i("wmy", "绚丽");
			data[7] = (byte) 0x40;
			intCmd = Util.byte2HexStr(data);
			strCmd = ConvertUtil.getInstance().transLightCmd(intCmd);
			// socket.sendMqttData(strCmd);
			sendCmd(strCmd);
			setBg(3);
			break;
		case R.id.scene_btn5:
			// Log.i("wmy", "流光");
			data[7] = (byte) 0x21;
			intCmd = Util.byte2HexStr(data);
			strCmd = ConvertUtil.getInstance().transLightCmd(intCmd);
			// socket.sendMqttData(strCmd);
			sendCmd(strCmd);
			setBg(4);
			break;
		case R.id.scene_btn6:
			// Log.i("wmy", "斑斓");
			data[7] = (byte) 0x20;
			intCmd = Util.byte2HexStr(data);
			strCmd = ConvertUtil.getInstance().transLightCmd(intCmd);
			// socket.sendMqttData(strCmd);
			sendCmd(strCmd);
			setBg(5);
			break;

		default:
			break;
		}
	}

	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				if (mSVProgressHUD.isShowing()) {
					mSVProgressHUD.dismiss();
				}
				needToCheck = false;
				mPowerB.setBackgroundResource(R.drawable.kaiguan12x);
				power = true;
				break;
			case 1:
				if (mSVProgressHUD.isShowing()) {
					mSVProgressHUD.dismiss();
				}
				needToCheck = false;
				mPowerB.setBackgroundResource(R.drawable.kaiguan22x);
				power = false;
				break;
			case 2:
				needToCheck = false;
				if (mSVProgressHUD.isShowing()) {
					mSVProgressHUD.dismiss();
				}
				break;
			case 4:
				if (mSVProgressHUD.isShowing()) {
					mSVProgressHUD.dismiss();
				}
				ToastShow.Show(getApplicationContext(), "获取状态失败：连接超时或设备未通电");
				break;
			default:
				break;
			}
		}
	};

	private void sendCmd(String cmd) {
		if (socket.isLocalSuccess()) {
			socket.sendMqttData(cmd);
			Log.i("wmy", "send local = " + cmd);
		} else {
			SocketManager.getInstance().sendRemoteData(socket.getMac(), device.getMainDeviceId(), cmd);
			Log.i("wmy", "send remote = " + cmd);
		}
	}

	@Override
	public void initParms(Bundle parms) {
		// TODO Auto-generated method stub

	}

	@Override
	public View bindView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int bindLayout() {
		// TODO Auto-generated method stub
		return R.layout.activity_rgb_light;
	}

	@Override
	public void initView(View view) {
		// TODO Auto-generated method stub
		mSVProgressHUD = new SVProgressHUD(RemoteRGBLightActivity.this);
		Log.i("wmy", "DeviceManager.getInstance().getDEVICE_DENSITY_DPI() = "
				+ DeviceManager.getInstance().getDEVICE_DENSITY_DPI());
		dpi = DeviceManager.getInstance().getDEVICE_DENSITY_DPI();
		o_x = 260 * dpi / 320;
		o_y = 261 * dpi / 320;
		mBackL = $(R.id.back_layout);
		mStatusBar = $(R.id.tv_statusBar_mian);
		mTitleTv = $(R.id.title_tv);
		mPowerB = $(R.id.power_b);
		// mMoreB = $(R.id.more_btn);
		mF = (RelativeLayout) findViewById(R.id.rl_father);
		Log.i("wmy", mF.getWidth() + " " + mF.getHeight());
		mF1 = (RelativeLayout) findViewById(R.id.rl_father1);
		mS = (RelativeLayout) findViewById(R.id.rl_mezi);
		Log.i("wmy", mS.getWidth() + " " + mS.getHeight());
		mS1 = (RelativeLayout) findViewById(R.id.rl_mezi1);
		sceneB1 = (Button) findViewById(R.id.scene_btn1);
		sceneB2 = (Button) findViewById(R.id.scene_btn2);
		sceneB3 = (Button) findViewById(R.id.scene_btn3);
		sceneB4 = (Button) findViewById(R.id.scene_btn4);
		sceneB5 = (Button) findViewById(R.id.scene_btn5);
		sceneB6 = (Button) findViewById(R.id.scene_btn6);
		sceneB1.setOnClickListener(this);
		sceneB2.setOnClickListener(this);
		sceneB3.setOnClickListener(this);
		sceneB4.setOnClickListener(this);
		sceneB5.setOnClickListener(this);
		sceneB6.setOnClickListener(this);
		buttons.add(sceneB1);
		buttons.add(sceneB2);
		buttons.add(sceneB3);
		buttons.add(sceneB4);
		buttons.add(sceneB5);
		buttons.add(sceneB6);
		lastTime = System.currentTimeMillis();
		currentTime = System.currentTimeMillis();
		lastTime1 = System.currentTimeMillis();
		currentTime1 = System.currentTimeMillis();
		d = mF.getBackground();
		bm = drawableToBitmap(d);
		pixel = bm.getPixel(248 * dpi / 320, 96 * dpi / 320);
		Log.i("wmy", "bm.getWidth() = " + bm.getWidth() + " bm.getHeight() = " + bm.getHeight());
		percent_ness = level / 100;
		rgb[0] = 128;
		rgb[1] = 255;
		rgb[2] = 0;
		rgb[3] = 0;
		address = getIntent().getStringExtra("address");
		Log.i("wmy", "address = " + address);
		deviceAdd = Util.hexStringToBytes(address);
		System.arraycopy(deviceAdd, 0, data, 1, 5);
		System.arraycopy(deviceAdd, 0, data1, 1, 5);
		intCmd = Util.byte2HexStr(data);
		intCmd1 = Util.byte2HexStr(data1);
		// p = getIntent().getIntExtra("position", 999);
		id = getIntent().getStringExtra("id");
		power = DeviceManager.getInstance().checkDeviceState(id);
		if (power) {
			mPowerB.setBackgroundResource(R.drawable.kaiguan12x);
		} else {
			mPowerB.setBackgroundResource(R.drawable.kaiguan22x);
		}
		device = DeviceManager.getInstance().getDeviceById(id);
		Log.i("wmy", "device = " + device.getDeviceName());
		mTitleTv.setText("彩灯：" + device.getDeviceName());
		socket = SocketManager.getInstance().sMap.get(DeviceManager.getInstance().getmWlanId());
		if (socket == null) {
			ToastShow.Show(getApplicationContext(), "没有找到所绑定网关，无法控制该设备!");
			finish();
		}
		checkStatus();
		boolVersion();
		mBackL.setOnClickListener(this);
		mPowerB.setOnClickListener(this);
		// mMoreB.setOnClickListener(this);
		mF.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent e) {
				// TODO Auto-generated method stub
				int action = e.getAction();
				switch (action) {
				case MotionEvent.ACTION_DOWN:
					if (adjustArea(o_x, o_y, e.getX(), e.getY())) {
						// Log.i("wmy", "点击" + e.getX() + " " + e.getY());
						current_degree = detaDegree(o_x, o_y, e.getX(), e.getY());
						// 根据象限求坐标
						currentTime = System.currentTimeMillis();
						getButtonXY(current_degree);
						mS.setX(tempX1 - 12);
						mS.setY(tempY1 - 12);
						mS.postInvalidate();
						// getColor();
					} else {
						return false;
					}
					break;
				case MotionEvent.ACTION_MOVE:
					if (adjustArea(o_x, o_y, e.getX(), e.getY())) {
						current_degree = detaDegree(o_x, o_y, e.getX(), e.getY());
						// 根据象限求坐标
						currentTime = System.currentTimeMillis();
						if (currentTime - lastTime > 150) {
							lastTime = currentTime;
							// getColor();
						}
						getButtonXY(current_degree);
						mS.setX(tempX1 - 12);
						mS.setY(tempY1 - 12);
						mS.postInvalidate();
					} else {
						return false;
					}
					break;
				case MotionEvent.ACTION_UP:
					Log.i("wmy", "上提" + e.getX() + " " + e.getY());
					// if (adjustArea(o_x, o_y, e.getX(), e.getY())) {
					current_degree = detaDegree(o_x, o_y, e.getX(), e.getY());
					// 根据象限求坐标
					getButtonXY(current_degree);
					mS.setX(tempX1 - 12);
					mS.setY(tempY1 - 12);
					mS.postInvalidate();
					getColor();
					// } else {
					// return false;
					// }
					break;

				default:
					break;
				}
				return true;
			}
		});

		mF1.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent e) {
				// TODO Auto-generated method stub

				switch (e.getAction()) {
				case MotionEvent.ACTION_DOWN:
					if (e.getX() < 100 * dpi / 320) {
						mS1.setX(100 * dpi / 320 - 30 * dpi / 320);
					} else if (e.getX() > 610 * dpi / 320) {
						mS1.setX(610 * dpi / 320 - 30 * dpi / 320);
					} else {
						mS1.setX(e.getX() - 30 * dpi / 320);
					}
					percent_ness = level / 100;
					// getNess();
					currentTime = System.currentTimeMillis();
					// Log.i("wmy", "按下坐标 x y = " + e.getX() + " " + e.getY());
					mS1.postInvalidate();
					break;
				case MotionEvent.ACTION_MOVE:
					if (e.getX() < 100 * dpi / 320) {
						mS1.setX(100 * dpi / 320 - 30 * dpi / 320);
						level = 0;
					} else if (e.getX() > 610 * dpi / 320) {
						mS1.setX(610 * dpi / 320 - 30 * dpi / 320);
						level = 100;
					} else {
						mS1.setX(e.getX() - 30 * dpi / 320);
						level = (int) ((e.getX() - 100 * dpi / 320) * 100 / (510 * dpi / 320));
					}
					currentTime = System.currentTimeMillis();
					if (currentTime - lastTime > 150) {
						lastTime = currentTime;
						percent_ness = level / 100;
					}
					mS1.postInvalidate();
					break;
				case MotionEvent.ACTION_UP:
					if (e.getX() < 100 * dpi / 320) {
						mS1.setX(100 * dpi / 320 - 30 * dpi / 320);
					} else if (e.getX() > 610 * dpi / 320) {
						mS1.setX(610 * dpi / 320 - 30 * dpi / 320);
					} else {
						mS1.setX(e.getX() - 30 * dpi / 320);
					}
					getNess();
					percent_ness = level / 100;
					mS1.postInvalidate();
					break;

				default:
					break;
				}
				return true;
			}
		});
	}

	@Override
	public void setListener() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doBusiness(Context mContext) {
		// TODO Auto-generated method stub

	}

	public void boolVersion() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

			Window window = getWindow();
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// 透明导航栏
			// getWindow().addFlags(
			// WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
			// 改变titlebar的高度
			int statusbarHeight = BarUtils.getStatusBarHeight(this);
			RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) mStatusBar.getLayoutParams();
			layoutParams1.height = statusbarHeight;
			mStatusBar.setLayoutParams(layoutParams1);
			mStatusBar.setVisibility(View.VISIBLE);
		} else {
			mStatusBar.setVisibility(View.GONE);
		}
	}

	public void checkStatus() {
		mSVProgressHUD.showWithStatus("正在获取设备状态");
		check();
		socket.setRecDataCallBackListener_Serach(new RecDataCallBackListener_Serach() {

			@Override
			public void onReceiveData(String data) {
				// TODO Auto-generated method stub
				received = true;
				parseRecData(data);
			}
		});
		SocketManager.getInstance().setOnDeviceStatusListener(new OnDeviceStatusListener() {

			@Override
			public void onStatus(String data, int what) {
				// TODO Auto-generated method stub
				received = true;
				mHandler.sendEmptyMessage(what);
			}
		});

//		mHandler.sendEmptyMessageDelayed(2, 10* 1000);
		checkTimeout();
	}
	
	private void checkTimeout(){
		mHandler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if(!received){
					mHandler.sendEmptyMessage(4);
				}
			}
		}, 10*1000);
	}

	private void check() {
		times++;
		if (times < 3 && needToCheck) {
			device.checkStatus();
		} else {
			return;
		}
		mHandler.postDelayed(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				check();
			}
		},  3000);
	}

	private void parseRecData(String data) {
		if (data.length() < 10 || data == null) {
			return;
		}
		JSONObject jo;
		try {
			jo = new JSONObject(data);
			String a = jo.getString("a");
			if (a.equals("20000")) {
				int msgId = jo.getInt("b");
				String g = jo.getString("g");
				JSONObject jo1 = new JSONObject(g);
				JSONArray jsonArray = jo1.optJSONArray("data");
				int dataja[] = new int[jsonArray.length()];
				if (jsonArray != null) {
					for (int i = 0; i < jsonArray.length(); i++) {
						dataja[i] = (Integer) jsonArray.get(i);
					}
					if (dataja.length == 8) {
						if (dataja[(jsonArray.length() - 1)] == 121) {
							// Log.i("wmy", "获取ID为" + msgId + "的设备的状态为开");
							DeviceManager.getInstance().setDeviceState(msgId + "", true);
							mHandler.sendEmptyMessage(0);
						} else if (dataja[(jsonArray.length() - 1)] == 120) {
							// Log.i("wmy", "获取ID为" + msgId + "的设备的状态为关");
							DeviceManager.getInstance().setDeviceState(msgId + "", false);
							mHandler.sendEmptyMessage(1);
						} else if (dataja[(jsonArray.length() - 1)] == 255) {
							// Log.i("wmy", "获取ID为" + msgId + "的设备的状态为开");
							DeviceManager.getInstance().setDeviceState(msgId + "", true);
							mHandler.sendEmptyMessage(0);
						} else if (dataja[(jsonArray.length() - 1)] == 0) {
							// Log.i("wmy", "获取ID为" + msgId + "的设备的状态为关");
							DeviceManager.getInstance().setDeviceState(msgId + "", false);
							mHandler.sendEmptyMessage(1);
						}
					} else if (dataja.length == 11) {
						if (dataja[7] == 255) {
							// Log.i("wmy", "获取ID为" + msgId + "的设备的状态为开");
							DeviceManager.getInstance().setDeviceState(msgId + "", true);
							mHandler.sendEmptyMessage(0);
						} else if (dataja[7] == 0) {
							// Log.i("wmy", "获取ID为" + msgId + "的设备的状态为关");
							DeviceManager.getInstance().setDeviceState(msgId + "", false);
							mHandler.sendEmptyMessage(1);
						}
					}
				}
			}

		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

	public static Bitmap drawableToBitmap(Drawable drawable) {
		Bitmap bitmap = Bitmap.createBitmap(520 * dpi / 320, 520 * dpi / 320,
				drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.ARGB_4444);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, 520 * dpi / 320, 520 * dpi / 320);
		// drawable.setBounds(0, 0, 520, 520);
		drawable.draw(canvas);
		return bitmap;
	}

	/**
	 * 计算以(src_x,src_y)为坐标圆点，建立直角体系，求出(target_x,target_y)坐标与x轴的夹角
	 * 主要是利用反正切函数的知识求出夹角
	 * 
	 * @param src_x
	 * @param src_y
	 * @param target_x
	 * @param target_y
	 * @return
	 */
	float detaDegree(float src_x, float src_y, float target_x, float target_y) {
		float detaX = target_x - src_x;
		float detaY = target_y - src_y;
		double d;
		if (detaX != 0) {
			float tan = Math.abs(detaY / detaX);

			if (detaX > 0) {

				if (detaY >= 0) {
					d = Math.atan(tan);

				} else {
					d = 2 * Math.PI - Math.atan(tan);
				}

			} else {
				if (detaY >= 0) {

					d = Math.PI - Math.atan(tan);
				} else {
					d = Math.PI + Math.atan(tan);
				}
			}

		} else {
			if (detaY > 0) {
				d = Math.PI / 2;
			} else {

				d = -Math.PI / 2;
			}
		}
		// Log.i("wmy", "夹角为" + (d * 180) / Math.PI);
		return (float) ((d * 180) / Math.PI);
	}

	private void getButtonXY(float degree) {
		tempX1 = (int) (o_x + (155 * dpi / 320 * Math.cos(Math.toRadians(degree))));
		tempY1 = (int) (o_y + (155 * dpi / 320 * Math.sin(Math.toRadians(degree))));
		// Log.e("wmy", "tempX1 tempY1 " + tempX1 + " " + tempY1);
	}

	private void getColor() {
		pixel = bm.getPixel(tempX1 - 12, tempY1 - 12);
		// Log.i("wmy", "tempX1 - 12 = " + (tempX1 - 12) + " tempY1 - 12 = " +
		// (tempY1 - 12));
		rgb[0] = 128;
		// rgb[1] = (int) (Color.red(pixel) * percent_ness);
		// rgb[2] = (int) (Color.blue(pixel) * percent_ness);
		// rgb[3] = (int) (Color.green(pixel * percent_ness);
		rgb[1] = (int) (Color.red(pixel));
		rgb[2] = (int) (Color.blue(pixel));
		rgb[3] = (int) (Color.green(pixel));
		// Log.i("wmy", "red green blue= " + rgb[1] + " " + rgb[2] + " " +
		// rgb[3]);
		System.arraycopy(rgb, 0, intCmd, 6, 4);
		strCmd = ConvertUtil.getInstance().transLightCmd(intCmd);
		sendCmd(strCmd);
		if (!power) {
			setBg(7);
			mPowerB.setBackgroundResource(R.drawable.kaiguan12x);
			power = !power;
		}

	}

	// 根据亮度的百分比
	// 以及当前rgb[]数组的值
	// pixel不变 计算颜色
	private void getNess() {
		Log.i("wmy", "percent_ness = " + percent_ness + " (int) (percent_ness * 255) = " + (int) (percent_ness * 255));
		if (percent_ness < 0.05) {
			intCmd1[7] = (int) (0.05 * 255);
		} else {
			intCmd1[7] = (int) (percent_ness * 255);
		}
		strCmd = ConvertUtil.getInstance().transLightCmd(intCmd1);
		mPowerB.setBackgroundResource(R.drawable.kaiguan12x);
		sendCmd(strCmd);
		if (!power) {
			setBg(7);
			mPowerB.setBackgroundResource(R.drawable.kaiguan12x);
			power = !power;
		}
	}

	private boolean adjustArea(float src_x, float src_y, float target_x, float target_y) {
		inner = (int) Math.sqrt(Math.pow(Math.abs(target_x - src_x), 2) + Math.pow(Math.abs(target_y - src_y), 2));
		if (inner < 260 * dpi / 320 && inner > 64 * dpi / 320) {
			return true;
		} else {
			return false;
		}
	}

	private void setBg(int p) {
		for (int i = 0; i < buttons.size(); i++) {
			buttons.get(i).setBackgroundResource(R.drawable.selector_scene1);
		}
		switch (p) {
		case 0:
			sceneB1.setBackgroundResource(R.drawable.kbt22x);
			mPowerB.setBackgroundResource(R.drawable.kaiguan12x);
			power = true;
			break;
		case 1:
			sceneB2.setBackgroundResource(R.drawable.kbt22x);
			mPowerB.setBackgroundResource(R.drawable.kaiguan12x);
			power = true;
			break;
		case 2:
			sceneB3.setBackgroundResource(R.drawable.kbt22x);
			mPowerB.setBackgroundResource(R.drawable.kaiguan12x);
			power = true;
			break;
		case 3:
			sceneB4.setBackgroundResource(R.drawable.kbt22x);
			mPowerB.setBackgroundResource(R.drawable.kaiguan12x);
			power = true;
			break;
		case 4:
			sceneB5.setBackgroundResource(R.drawable.kbt22x);
			mPowerB.setBackgroundResource(R.drawable.kaiguan12x);
			power = true;
			break;
		case 5:
			sceneB6.setBackgroundResource(R.drawable.kbt22x);
			mPowerB.setBackgroundResource(R.drawable.kaiguan12x);
			power = true;
			break;

		default:
			break;
		}
	}

}
